using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace HZY.Admin.Services
{
    using System.Threading.Tasks;
    using HZY.EFCore.Repository;
    using HZY.Models;
    using HZY.Models.Sys;
    using HZY.Toolkits;
    using HZY.EFCore.Base;
    using HZY.Admin.Services.Core;
    using HZY.EFCore;
    using Microsoft.EntityFrameworkCore;

    /// <summary>
    /// 订单明细
    /// </summary>
    public class SalesSlipInfo_ItemsService : ServiceBase<SalesSlipInfo_Items>
    {
        /// <summary>
        /// 产品
        /// </summary>
        private readonly DefaultRepository<Product> productDb;
        private readonly DefaultRepository<Customer> customerDb;
        private readonly DefaultRepository<SalesSlip> salesslipDb;
       
        public SalesSlipInfo_ItemsService(EFCoreContext _db, DefaultRepository<Product> productDb, DefaultRepository<Customer> customerDb, DefaultRepository<SalesSlip> salesslipDb) 
            : base(_db)
        {
            this.productDb = productDb;
            this.customerDb = customerDb;
            this.salesslipDb = salesslipDb;
        }

        #region CURD 基础

        /// <summary>
        /// 获取数据源
        /// </summary>
        /// <param name="Page"></param>
        /// <param name="Rows"></param>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<TableViewModel> FindListAsync(int Page, int Rows, SalesSlipInfo_Items Search)
        {
            var query = this.Query()
                //.WhereIF(w => w.Function_Name.Contains(Search.Function_Name), !string.IsNullOrWhiteSpace(Search?.Function_Name))
                //.OrderBy(w => w.Function_Num)
                .Select(w => new
                {
                    w.Master_Id,w.SalesSlipInfo_Items_ProdectId,w.SalesSlipInfo_Items_ProdectColour,w.SalesSlipInfo_Items_ProdectNumber,w.SalesSlipInfo_Items_ProdectUnit,w.SalesSlipInfo_Items_ProdecUnitPrice,w.SalesSlipInfo_Items_ProdecMoney,w.SalesSlipInfo_Items_CreateTime,_ukid = w.SalesSlipInfo_Items_ID
                })
                ;

            return await this.db.AsTableViewModelAsync(query, Page, Rows, typeof(SalesSlipInfo_Items));
        }

        /// <summary>
        /// 新增\修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<Guid> SaveAsync(SalesSlipInfo_Items model)
        {
          
            await this.InsertOrUpdateAsync(model);

            return model.SalesSlipInfo_Items_ID;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(List<Guid> Ids)
            => await this.DeleteAsync(w => Ids.Contains(w.SalesSlipInfo_Items_ID));

        /// <summary>
        /// 加载表单 数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, object>> LoadFormAsync(Guid Id)
        {
            var res = new Dictionary<string, object>();

            var Model = await this.FindByIdAsync(Id);
            var ItemsList = await this.Query().Where(c=>c.SalesSlipInfo_Items_Enclosure!=null).GroupBy(v=>new { SalesSlipInfo_Items_Enclosure =v.SalesSlipInfo_Items_Enclosure }).Select(g=>new { value = g.Key.SalesSlipInfo_Items_Enclosure}).ToListAsync();
            var productList = await this.productDb.Query().OrderBy(o => o.Product_Code).ToListAsync();
            res[nameof(Id)] = Id;
            res[nameof(Model)] = Model.NullSafe();
            res[nameof(productList)] = productList.NullSafe();
            res[nameof(ItemsList)] = ItemsList.NullSafe();
            return res;
        }
        /// <summary>
        /// 订单打印
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<Dictionary<string, object>> ItmesFindList(Guid Id) {
            var res = new Dictionary<string, object>();
            var salesslipModel = await (
             from salesslip in this.db.SalesSlips.Where(v => v.SalesSlip_ID == Id)
             from customer in this.db.Customers.Where(v => v.Customer_ID == salesslip.SalesSlip_CustomerId)
             select new { salesslip, customer }
             )
              .OrderBy(w => w.salesslip.SalesSlip_Code)
             .Select(w => new
             {
                 w.salesslip.SalesSlip_Name,
                 w.salesslip.SalesSlip_Code,
                 w.customer.Customer_Code,
                 w.customer.Customer_Name,
                 w.customer.Costomer_Address,
                 w.customer.Customer_Phone,
                 w.salesslip.SalesSlip_EntryTime,
                 w.salesslip.SalesSlip_CreateTime,
                 w.salesslip.SalesSlip_Handler,
                 w.salesslip.SalesSlip_IsComplete,
                 w.salesslip.SalesSlip_Remarks,
                 
                 _ukid = w.salesslip.SalesSlip_ID 
                 
             }).ToListAsync();

            var salesslipItmesList = await ( 
                from products in this.db.Products
                from salesslipItemss in this.db.SalesSlipInfo_Itemss.Where(s=>s.SalesSlipInfo_Items_ProdectId==products.Product_ID)
                select new { products, salesslipItemss }
                
                ) .Where(w=> w.salesslipItemss.Master_Id == Id)
                 
                .OrderBy(v=>v.products.Product_Code)
                 
                .Select(w => new
                {
                    w.products.Product_Code,
                    w.products.Product_Name,
                    w.products.Product_Colour,
                    w.products.Product_Specs,
                    w.salesslipItemss.SalesSlipInfo_Items_Enclosure,
                    w.salesslipItemss.SalesSlipInfo_Items_ProdectUnit,
                    w.salesslipItemss.SalesSlipInfo_Items_ProdecUnitPrice,
                    w.salesslipItemss.SalesSlipInfo_Items_ProdectNumber,
                    w.salesslipItemss.SalesSlipInfo_Items_Width,
                    w.salesslipItemss.SalesSlipInfo_Items_Height,
                    w.salesslipItemss.SalesSlipInfo_Items_ProdecMoney
                    
                    
                }).ToListAsync();
            res[nameof(salesslipModel)] = salesslipModel.NullSafe();
            res[nameof(salesslipItmesList)] = salesslipItmesList.NullSafe();
            return res;
        }
        
        #endregion

        #region 导出 Excel

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="Search"></param>
        /// <returns></returns>
        public async Task<byte[]> ExportExcel(SalesSlipInfo_Items Search)
        {
            var tableViewModel = await this.FindListAsync(1, 999999, Search);
            return this.ExportExcelByTableViewModel(tableViewModel);
        }

        #endregion

    }
}
